package com.exercise.struct.base

import android.os.Bundle
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.exercise.struct.R
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.snackbar.Snackbar

abstract class BaseDataBindingActivity<VDB : ViewDataBinding> : AppCompatActivity() {
    protected lateinit var mDataBinding: VDB
    protected lateinit var mToolbar: MaterialToolbar

    @get:LayoutRes
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBinding = DataBindingUtil.setContentView(this, layoutResId)
        mToolbar = findViewById(R.id.toolbar)
        initView()
        setToolbar()
        setListener()
    }

    private fun setListener() {
        mToolbar.setNavigationOnClickListener {
            finish()
        }
        mToolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.action_author -> {
                    Snackbar.make(mDataBinding.root, "作者：qingYun", Snackbar.LENGTH_SHORT)
                        .setAction("记住了") {
                            Toast.makeText(this, "生为蝼蚁，当有鸿鹄之志!", Toast.LENGTH_SHORT)
                                .show()
                        }
                        .show()
                }

                R.id.action_share -> {
                    Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show()
                }

                R.id.action_settings -> {
                    Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show()
                }
            }
            return@setOnMenuItemClickListener true
        }
    }

    abstract fun setToolbar()

    abstract fun initView()

    protected fun initToolbar(title: String) {

    }
}